package org.itboys.coo.service.mongo.meal;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.coo.conmon.Finals;
import org.itboys.coo.entity.mongo.meal.CooMeal;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/11/30
 * 描述：迈迪龙_套餐
 */
@Service
public class CooMealService extends BaseMongoService<CooMeal>{
    @Resource(name="mdlDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<CooMeal> getEntityClass() {
        return CooMeal.class;
    }
    @Autowired
    private SundayUploadService uploadService;
    /**
     * 查询
     * @param request
     * @return
     */
    public PageResult<CooMeal> selectMeal(HttpServletRequest request){
        Map<String ,Object> param= ParamUtil.packageMongoExactParam(request);
        Map<String ,String> containparam=ParamUtil.packageMongoVagueParam(request);
        param.put("isDeleted", Finals.NO);
        return super.containpageQuery(request,containparam,param);
    }

    /**
     * 保存  或修改套餐
     * @param meal
     * @throws IOException
     */
    public void saveMeal(CooMeal meal, MultipartFile imageFile)  throws  Exception{
        //保存主图
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            meal.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(meal.getId())){
            super.updateExceptEmpty(meal.getId(),meal);
        }else {
            super.save(meal);
        }
    }

    /**
     * 查询单个
     * @param mealId
     * @return
     */
    public CooMeal findOne(Long mealId){
        CooMeal meal=null;
        if(LongUtil.isNotZreo(mealId)){
            meal=super.getById(mealId);
        }else {
            meal=new CooMeal();
        }
        return meal;
    }

    /**
     * 删除套餐
     * @param mealId
     */
    public void deleteMeal(Long mealId){
        super.delete(mealId);
        logger.info("删除套餐","id="+mealId);
    }
}
